package com.foodplus.www.view.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.foodplus.www.adapter.special.SpecialChildAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.special.ArticleListBean;
import com.foodplus.www.bean.special.DatasBean;
import com.foodplus.www.bean.special.SpecialBean;
import com.foodplus.www.foodplus.MyApp;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.SpecialPresenter;
import com.foodplus.www.view.impl.SpecialView;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpecialChildFragment extends Fragment implements SpecialView {

    @BindView(R.id.special_RecyclerView)
    RecyclerView specialRecycler;
    private List<ArticleListBean> data = new ArrayList<>();

    private static int[] specialType = {1,2,3,4,5,6};
    int mSpecialType;

    private SpecialChildAdapter specialChildAdapter;

    private SpecialPresenter specialPresenter;


    public SpecialChildFragment() {
        // Required empty public constructor
    }

    public static SpecialChildFragment newInstance(int i) {
        Bundle args = new Bundle();
        SpecialChildFragment fragment = new SpecialChildFragment();
        args.putInt("mSpecialType",specialType[i]);
        fragment.setArguments(args);
        return fragment;
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_special_child, container, false);
        ButterKnife.bind(this, view);
         mSpecialType = getArguments().getInt("mSpecialType");
        initView();
        initData();
        return view;
    }


    private void initView() {
        specialChildAdapter = new SpecialChildAdapter(data,getContext(), mSpecialType);
        specialRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        specialRecycler.setAdapter(specialChildAdapter);
    }

    private void initData() {
        specialPresenter = new SpecialPresenter(this);
        Map<String , String> map = ParamManager.getInstance().getSpecialMap(mSpecialType);
        specialPresenter.getSpecialData(map);
    }



    @Override
    public void onSuccess(SpecialBean specialBean) {
        data.addAll(specialBean.getDatas().getArticle_list());
        specialChildAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {
        System.out.println();
        Toast.makeText(getContext(), "网络不给力啊，请稍后再试", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFinish() {

    }
}
